package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.alibaba.fastjson.JSONObject;
import com.bean.*;
import com.service.*;
public class routeaction  extends ActionSupport {
	
	private Route route;
	private routeService<Route> rs;
	private List<Route> routes;
	private String result;
	

	public void AddRoute(){
		try {
			rs.AddRoute(route);
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("<script>alert('Add New Route Successfully')</script>");
			out.print("<script>history.go(-1)</script>");
			out.flush();
			out.close();
		}
			
	catch (Exception e) {
		// TODO Auto-generated catch block			
		try {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("<script>alert('Wrong Message:This Route Number has existed already!')</script>");
			out.print("<script>history.go(-1)</script>");
			out.flush();
			out.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		e.printStackTrace();
	}
    }	
	public String SelectRoute(){ 
		try {
			   
			   String str="From Route";
			   if(!route.getRno().equals(""))
			str="From Route route WHERE route.rno='"+route.getRno()+"'";			      
		    routes = rs.SelectRoute(str);
		    result=toJson(routes);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block

			result = null;
			}
		return SUCCESS;
	}
	public void UpdateRoute(){
		try {
			rs.UpdateRoute(route);
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("<script>alert('Update Infomation Successfully')</script>");
			out.print("<script>history.go(-1)</script>");
			out.flush();
			out.close();
		}
			
	catch (Exception e) {
		// TODO Auto-generated catch block			
		try {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("<script>alert('Wrong Message:Update failed')</script>");
			out.print("<script>history.go(-1)</script>");
			out.flush();
			out.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		e.printStackTrace();
	}
	}
	
	
	private String toJson(Object obj){
		JSONObject jsonobj = new JSONObject();
		return jsonobj.toJSON(obj).toString();
		
	}
	public List<Route> getRoutes() {
		return routes;
	}

	public void setRoutes(List<Route> routes) {
		this.routes = routes;
	}

	public Route getRoute() {
		return route;
	}
	public void setRoute(Route route) {
		this.route = route;
	}
	public routeService<Route> getRs() {
		return rs;
	}
	public void setRs(routeService<Route> rs) {
		this.rs = rs;
	}   
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
}
